// @Title customer[简述该包的主要作用]
// @Description
// @Author  cnHuaShao
// @since v0.0.1
// @Version v1.0.0
// @Date 2024/5/22 23:52
// @Update 2024/5/22 23:52
package customer

import (
	"FinancialManagement/src/base/db"
	"errors"
	"fmt"
	"gorm.io/gorm"
)

// DelCustomerInfo
// @Description: 删除客户信息
// @param customerId
// @return error
func DelCustomerInfo(customerId int) error {
	return db.Res.DB.GetDbW().Delete(&Customer{}, customerId).Error
}

// UpdateCustomerInfo
// @Description: 修改客户信息
// @param customerID 客户ID
// @param name 客户姓名
// @param address 地址
// @param description 描述
// @param gender 性别
// @param provinceId 省份ID
// @param cityId 城市ID
// @return error
func UpdateCustomerInfo(customerID int, name, address, description string, gender, provinceId, cityId int) error {
	var customer Customer
	result := db.Res.DB.GetDbW().First(&customer, customerID)
	if errors.Is(result.Error, gorm.ErrRecordNotFound) {
		return errors.New("找不到该客户信息")
	}
	if name != "" {
		customer.Name = name
	}
	if address != "" {
		customer.Address = address
	}
	if gender == 1 || gender == 2 {
		customer.Gender = gender
	}
	if description != "" {
		customer.Description = description
	}
	if provinceId != 0 {
		customer.ProvinceID = provinceId
	}
	if cityId != 0 {
		customer.CityID = cityId
	}
	result = db.Res.DB.GetDbW().Save(&customer)
	return result.Error
}

// CreateCustomerInfo
// @Description: 创建客户信息
// @param customer
// @return error
func CreateCustomerInfo(customer Customer) error {
	return db.Res.DB.GetDbW().Create(&customer).Error
}

// GetCustomerInfo
// @Description: 获取指定客户信息
// @param customerID
// @return *Customer
// @return error
func GetCustomerInfo(customerID int) (*Customer, error) {
	customer := Customer{}
	err := db.Res.DB.GetDbR().Where("customer_id = ?", customerID).First(&customer).Error
	return &customer, err
}

// GetCustomerListAll
// @Description: 获取所有客户信息，不分页
// @param provinceId 省份ID
// @param cityId 市区ID
// @param customerType 客户类型
// @return []Customer
// @return error
func GetCustomerListAll(provinceId, cityId int, customerType int) ([]Customer, error) {
	var customerList []Customer
	sqlWhere := " 1=1 "
	if provinceId != 0 {
		sqlWhere = fmt.Sprintf("%s and province_id = %d", sqlWhere, provinceId)
	}
	if cityId != 0 {
		sqlWhere = fmt.Sprintf("%s and city_id = %d", sqlWhere, cityId)
	}
	if customerType != 0 {
		sqlWhere = fmt.Sprintf("%s and customer_type = %d", sqlWhere, customerType)
	}
	result := db.Res.DB.GetDbR().Order("customer_id").Where(sqlWhere).Find(&customerList)
	return customerList, result.Error
}

// GetCustomerList
// @Description: 获取所有客户信息
// @param name 搜索客户姓名
// @param provinceId 所属省份
// @param cityId 所属城市
// @return []Customer
// @return error
func GetCustomerList(name string, provinceId, cityId int, pageSize, pageNumber int, customerType int) ([]Customer, int64, error) {
	var customerList []Customer
	sqlWhere := " 1=1 "
	if name != "" {
		sqlWhere = fmt.Sprintf("%s and name like '%%%s%%'", sqlWhere, name)
	}
	if provinceId != 0 {
		sqlWhere = fmt.Sprintf("%s and province_id = %d", sqlWhere, provinceId)
	}
	if cityId != 0 {
		sqlWhere = fmt.Sprintf("%s and city_id = %d", sqlWhere, cityId)
	}
	if customerType != 0 {
		sqlWhere = fmt.Sprintf("%s and customer_type = %d", sqlWhere, customerType)
	}
	// 计算起始行
	startPage := (pageNumber - 1) * pageSize
	var count int64
	result := db.Res.DB.GetDbR().Order("customer_id").Where(sqlWhere).Limit(pageSize).Offset(startPage).Find(&customerList).Limit(-1).Offset(-1).Count(&count)
	return customerList, count, result.Error
}
